﻿using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Threading.Tasks;
namespace Zeus.Mvc.Loggers.Implementations
{
    public class HttpContextLogger : DefaultHttpContextLogger
    {
        private ILogger _logger;

        public HttpContextLogger(ILogger<HttpContextLogger> logger)
        {
            _logger = logger;
        }

        protected override async Task<IHttpContextModel> BuildResponseModel(HttpContext httpContext)
        {
            var logModel = await base.BuildResponseModel(httpContext);
            _logger.LogInformation(JsonConvert.SerializeObject(logModel));
            return logModel;
        }

        public override Task ExceptionLog(HttpContext context, Exception ex)
        {
            _logger.LogError(ex, ex.Message, null);
            return base.ExceptionLog(context, ex);
        }
    }
}
